package labb3.FileHandling;

import labb3.Media;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @author Mikko Puustinen
 * @version 0.1, 2007-okt-14
 */
public class LoadXML implements CollectionLoader {
    private String path;

    //Not implemented
    private LoadXML(String path) {
        this.path = path;
    }

    @SuppressWarnings({"unchecked"})
    public ArrayList<Media> loadCollection() throws IOException, ClassNotFoundException {
        ArrayList<Media> media;

        XMLDecoder decoder = null;
        try {
            decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(path)));
        } catch (FileNotFoundException e) {
            media = new ArrayList<Media>();
            throw new IOException("File not found");
        }
        if (decoder != null) {
            media = (ArrayList<Media>)decoder.readObject();
        } else {
            media = new ArrayList<Media>();
            System.out.println("NULL!");
        }
        if (decoder != null) {
            decoder.close();
        }
        return media;
    }
}
